/*
 *  pc.c
 *  
 *
 *  Created by Rayvionne French on 7/24/11.
 *  Copyright 2011 __MyCompanyName__. All rights reserved.
 *
 */

#include "syscall.h"

int data;
int lock;
int cv;
int start;
int go;

void ProducerConsumer() {
	int value = 5;
	int begin = 0;
	
	while(begin == 0) {
		LockAcquire(start);
		if(RetrieveIntMV(go, 0) != 0) {
			begin = 1;
		}
		LockRelease(start);
		Yield();
	}
	
	LockAcquire(lock);
	Write("PC writing orignal value.\n", sizeof("PC writing orignal value.\n"), ConsoleOutput);
	AlterIntMV(data, 0, value);
	CVSignal(cv, lock);
	CVWait(cv, lock);
	Write("PC reading new value.\n", sizeof("PC writing orignal value.\n"), ConsoleOutput);
	value = RetrieveIntMV(data, 0);
	LockRelease(lock);
	
	Exit(value);
}

int main() {
	/* Initialize the shared data */
	data = CreateIntMV("data", 4, 1);
	AlterIntMV(data, 0, 0);
	
	lock = CreateLock("lock", 4);
	cv = CreateCV("cv", 2);
	
	go = CreateIntMV("go", 2, 1);
	AlterIntMV(go, 0, 0);
	start = CreateLock("start", 5);
	
	Fork(&ProducerConsumer, "ProducerConsumer", 16);
	
	Exit(-1);
}